package com.edu.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.common.utils.R;
import com.edu.eduservice.entity.EduCourse;
import com.edu.eduservice.service.EduChapterService;
import com.edu.eduservice.service.EduCourseDescriptionService;
import com.edu.eduservice.service.EduCourseService;
import com.edu.eduservice.service.EduSubjectService;
import com.edu.eduservice.vo.*;
import com.edu.eduservice.vo.chapter.ChapterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-05-30
 */
@Api(description = "课程管理")
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;

    /**
     * 查询课程
     * @param courseId
     * @return
     */
    @ApiOperation(value = "根据ID查询课程")
    @GetMapping(value = "/{courseId}")
    public R getById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId){

        //查询课程信息和讲师信息
        CourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);

        //查询当前课程的章节信息
        List<ChapterVo> chapterVideoByCourseId = chapterService.getChapterVideoByCourseId(courseId);

        return R.ok().data("course",courseWebVo).data("chapterVoList",chapterVideoByCourseId);

    }








    /**
     * 条件查询 !带分页web课程列表显示!
     * @param page
     * @param limit
     * @param courseQuery
     * @return
     */
    @ApiOperation(value = "分页课程列表")
    @PostMapping(value = "/{page}/{limit}")
    public R pageList(@ApiParam(name = "page",value = "page",required = true)
                      @PathVariable Long page,
                      @ApiParam(name = "limit", value = "每页记录数", required = true)
                      @PathVariable Long limit,
                      @ApiParam(name = "courseQuery", value = "查询对象", required = false)
                          @RequestBody(required = false) CourseQueryVo courseQuery){
        Page<EduCourse> pageParam = new Page<>(page, limit);

        Map<String,Object>map=courseService.pageListWeb(pageParam,courseQuery);
        return R.ok().data(map);
    }







    /**
     * 课程列表
     * TODO条件查询带分类
     */
    @GetMapping
    public  R getCourseList(){

        List<EduCourse> list = courseService.list(null);
        return  R.ok().data("courseList",list);
    }


    /**
     * 条件分页查询
     * @param page
     * @param limit
     * @param courseQuery
     * @return
     */
        @ApiOperation("分页条件查询课程信息")
        @PostMapping("/queryCourse/{page}/{limit}")
    public R getCoureseListQuery(@ApiParam(name = "当前页码",value = "page",required = true)
                                         @PathVariable("page") Long page,
                                 @ApiParam(name = "每页记录数",value = "limit",required = true)
                                         @PathVariable("limit") Long limit,
                                 @ApiParam(name = "查询对象",value = "courseQuery",required = true)
                                         @RequestBody(required = false) CourseQuery courseQuery){
            //创建分页对象
            Page<EduCourse> coursePage = new Page<>(page,limit);

            //调用方法实现多条件分页查询
            courseService.pageQuery(coursePage,courseQuery);

            //获取到查询的结果
            List<EduCourse> records = coursePage.getRecords();
            long total = coursePage.getTotal();
            System.out.println(records.toString());

            return  R.ok().data("total",total).data("rows",records);
    }





    /**
     * 修改课程
     * @param courseInfoVo
     * @return
     */
    @ApiOperation("修改课程")
    @PostMapping("/updateCourseInfo")
    @Transactional
    public R updateCoureseInfo(@ApiParam(value = "courseInfoVo",name = "课程",required = true)
                               @RequestBody CourseInfoVo courseInfoVo){
                courseService.updateCoureseInfo(courseInfoVo);

        return R.ok();
    }





    /**
     * 查询课程
     * @param courseId
     * @return
     */
    @ApiOperation(value = "查询课程")
    @GetMapping("/getCourse/{courseId}")
    public R getCourese(@ApiParam(value = "courseId",name = "课程id",required = true)
                        @PathVariable("courseId") String courseId){

        CourseInfoVo courseInfoVo = courseService.getCoureseInfo(courseId);

        return R.ok().data("course",courseInfoVo);
    }






    /**
     * 新增课程
     * @param courseInfoVo
     * @return
     */
    @ApiOperation(value = "新增课程")
    @PostMapping("/savecourse")
    @Transactional
    public R saveCourse(
                        @ApiParam(name = "课程表单",value = "courseInfoVo",required = true)
                        @RequestBody CourseInfoVo courseInfoVo){
          String CoureseId =  courseService.saveCourseInfo(courseInfoVo);
          if (!StringUtils.isEmpty(CoureseId)){
              return R.ok().data("courseId",CoureseId);
          }else{
              return R.ok().message("保存失败!");
          }

    }

    /**
     * 根据id查询课程
     * @param courseId
     * @return
     */
    @ApiOperation("根据id查询课程vo")
    @GetMapping("/publicCourseVo/{courseId}")
    public R queyrByCourseId(@ApiParam(value = "courseId",name = "courseId",required = true)
                                 @PathVariable("courseId") String courseId){
           CoursePublishVo coursePublishVo= courseService.publicCourseInfo(courseId);

        return R.ok().data("coursePublicVo",coursePublishVo);
    }


    /**
     * 发布课程
     */
    @ApiOperation("发布课程")
    @PostMapping("/publishCourse/{id}")
    @Transactional
    public R publishCourse(@ApiParam(value = "id",name = "课程id",required = true)
                        @PathVariable String id){
        EduCourse eduCourse =new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);

        return R.ok();
    }


    /**
     * 删除课程
     */
    @ApiOperation("删除课程")
    @DeleteMapping("/{courseId}")
    public R delCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);

        return R.ok();
    }




}

